﻿using System;
using System.Windows.Controls;
using GalaSoft.MvvmLight.Messaging;

namespace Songhay.Silverlight.Mvvm.Views
{
    using Songhay.Models;
    using Songhay.Mvvm.Models;

    /// <summary>
    /// Generic alert window
    /// </summary>
    public partial class AlertChildWindow : ChildWindow
    {
        /// <summary>
        /// Initializes a new instance
        /// of the <see cref="AlertChildWindow"/> class.
        /// </summary>
        public AlertChildWindow()
        {
            InitializeComponent();

            #region eventing:

            this.Closed += (s, args) =>
            {
                this.Panel.Children.Clear();

                if(this._isClosing) return;

                this._isClosing = true;
                var message = new LightMessage<ChildWindow>(this)
                {
                    DisplayText = (string)this.Title,
                    Source = LightMessageSource.ViewEventClosing,
                    ViewId = "AlertChildWindow"
                };
                Messenger.Default.Send(message);
            };

            this.OKButton.Click += (s, args) =>
            {
                this.DialogResult = true;
            };

            #endregion

            #region messaging:

            Messenger.Default.Register<LightNotificationMessageAction<ChildWindow>>(this,
            message =>
            {
                message.Execute(this);
            });

            #endregion
        }

        /// <summary>
        /// Initializes a new instance
        /// of the <see cref="AlertChildWindow"/> class.
        /// </summary>
        /// <param name="control">The control.</param>
        public AlertChildWindow(UserControl control)
            : this()
        {
            if(control == null)
                throw new ArgumentNullException("control",
                    "The control to targetValues is null.");

            if((control.Height >= this.Height) || (control.Width >= this.Width))
            {
                this.Height = (control.Height * 1.25);
                this.Width = (control.Width * 1.25);
            }

            if(control.Tag is string) this.Title = control.Tag.ToString();
            this.Panel.Children.Add(control);

            #region messaging:

            var message = new LightMessage<AlertChildWindow>(this)
            {
                DisplayText = (string)this.Title,
                Source = LightMessageSource.ViewAddedChildren,
                ViewId = "AlertChildWindow"
            };
            Messenger.Default.Send(message);

            #endregion
        }

        /// <summary>
        /// <c>true</c> when the <see cref="ChildWindow.Closing"/> event is called for the first time.
        /// </summary>
        /// <remarks>
        /// A few debugging sessions suggest that the <see cref="ChildWindow.Closing"/> event fires more than once.
        /// </remarks>
        bool _isClosing;
    }
}

